<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>C语言：笔记202305 |  朝花夕拾</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/ww.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-C语言：笔记202305"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  C语言：笔记202305
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/23/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E7%AC%94%E8%AE%B0202305/" class="article-date">
  <time datetime="2023-05-23T06:17:08.000Z" itemprop="datePublished">2023-05-23</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/C%E8%AF%AD%E8%A8%80/">C语言</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">4k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">19 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="C语言：笔记202305"><a href="#C语言：笔记202305" class="headerlink" title="C语言：笔记202305"></a>C语言：笔记202305</h1><h2 id="关键字extern"><a href="#关键字extern" class="headerlink" title="关键字extern"></a>关键字extern</h2><p>​	extern关键字：再找找别急着报错。</p>
<h2 id="变量作用域-scope"><a href="#变量作用域-scope" class="headerlink" title="变量作用域(scope)"></a>变量作用域(scope)</h2><ol>
<li><p>代码块作用域  ：{}</p>
</li>
<li><p>文件作用域      ：写的地方到文件结束</p>
</li>
<li><p>原型作用域      ：函数原型声明的时候的变量名可以随便 写</p>
</li>
<li><p>函数作用域      ：配合goto语句的标签</p>
</li>
</ol>
<h2 id="链接属性"><a href="#链接属性" class="headerlink" title="链接属性"></a>链接属性</h2><p>​	编译器编译c源码和基础库做链接，生成可执行程序。</p>
<ul>
<li>external   可以去别的文件里面去找具有文件作用域的对象</li>
<li>internal    用static关键字来限制external对象，限制后只能本文件内访问</li>
<li>none         代码块变量是空链接属性</li>
</ul>
<h2 id="生存期"><a href="#生存期" class="headerlink" title="生存期"></a>生存期</h2><ul>
<li>静态存储期 （static storage duration）：文件作用域，程序退出释放。</li>
<li>自动存储期 （automatic storage duration）： 代码块作用域，代码块结束时效</li>
</ul>
<h2 id="变量存储类型"><a href="#变量存储类型" class="headerlink" title="变量存储类型"></a>变量存储类型</h2><ol>
<li>auto         ：自动存储区，代码块中的变量默认是atuo类型</li>
<li>register   ： 自动存储区，寄存器变量是有可能存储在cpu内部的</li>
<li>static       ： 静态存储区，静态局部变量的生存期和全局变量一样但是作用域还是块</li>
<li>extern     ： 声明外部再找找</li>
<li>typedef   ： 对类型的封装，给类型取别名，配合结构体使用更方便</li>
</ol>
<h2 id="关键字struct"><a href="#关键字struct" class="headerlink" title="关键字struct"></a>关键字struct</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Date</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> year;</span><br><span class="line">    <span class="type">int</span> mouth;</span><br><span class="line">    <span class="type">int</span> day;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Date</span> *<span class="title">date</span>;</span></span><br><span class="line">    date = (<span class="keyword">struct</span> Date *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Date));</span><br><span class="line">    <span class="keyword">if</span> (date == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;内存分配失败\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    date-&gt;year = <span class="number">2023</span>;</span><br><span class="line">    date-&gt;mouth = <span class="number">5</span>;</span><br><span class="line">    date-&gt;day = <span class="number">20</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d-%d-%d&quot;</span>, date-&gt;year, date-&gt;mouth, date-&gt;day);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="关键字typedef"><a href="#关键字typedef" class="headerlink" title="关键字typedef"></a>关键字typedef</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">Date</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> year;</span><br><span class="line">    <span class="type">int</span> mouth;</span><br><span class="line">    <span class="type">int</span> day;</span><br><span class="line">&#125; DATE, *P_DATE;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    P_DATE date;</span><br><span class="line">    date = (P_DATE)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(DATE));</span><br><span class="line">    <span class="keyword">if</span> (date == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;内存分配失败\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    date-&gt;year = <span class="number">2023</span>;</span><br><span class="line">    date-&gt;mouth = <span class="number">5</span>;</span><br><span class="line">    date-&gt;day = <span class="number">20</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d-%d-%d&quot;</span>, date-&gt;year, date-&gt;mouth, date-&gt;day);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="指针函数和函数指针"><a href="#指针函数和函数指针" class="headerlink" title="指针函数和函数指针"></a>指针函数和函数指针</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    函数指针            int (*fp)(int, int)</span></span><br><span class="line"><span class="comment">    函数指针作入参       </span></span><br><span class="line"><span class="comment">    函数指针作返回值  </span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">sub</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">compt</span><span class="params">(<span class="type">int</span> (*)(<span class="type">int</span>,<span class="type">int</span>),<span class="type">int</span>,<span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> (*select(<span class="type">char</span>))(<span class="type">int</span>, <span class="type">int</span>);</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a+b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">sub</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a-b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数指针作入参的例子</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">compt</span><span class="params">(<span class="type">int</span> (*fp)(<span class="type">int</span>,<span class="type">int</span>),<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (*fp)(a,b);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数指针作返回值的例子</span></span><br><span class="line"><span class="type">int</span> (*select(<span class="type">char</span> o))(<span class="type">int</span> a,<span class="type">int</span> b)&#123;</span><br><span class="line">    <span class="keyword">switch</span> (o)</span><br><span class="line">    &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;+&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> add;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;-&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> sub;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> rest;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 函数指针作入参的例子</span></span><br><span class="line">    rest = compt(add,<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a+b = %d\n&quot;</span>,rest);</span><br><span class="line">    rest = compt(sub,<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a-b = %d\n&quot;</span>,rest);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 函数指针作返回值的例子</span></span><br><span class="line">    <span class="type">char</span> o;</span><br><span class="line">    <span class="type">int</span> a,b;</span><br><span class="line">    <span class="type">int</span> (*fp)(<span class="type">int</span>, <span class="type">int</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入计算式（例如2+3）：&quot;</span>);</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%c%d&quot;</span>,&amp;a,&amp;o,&amp;b);</span><br><span class="line">    fp = select(o);</span><br><span class="line">    rest = compt(fp,a,b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;rest = %d\n&quot;</span>,rest);   </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h2><p>​	函数调用它本身。递归必须要有结束条件，否则程序会崩溃。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"></span><br><span class="line">void func01(int);</span><br><span class="line"></span><br><span class="line">void func01(int n)</span><br><span class="line">&#123;</span><br><span class="line">    printf(&quot;%d aaaa\n&quot;,n);</span><br><span class="line">    if (--n)</span><br><span class="line">    &#123;</span><br><span class="line">        func01(n);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">    func01(3);</span><br><span class="line">    func01(3);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"></span><br><span class="line">long func01(int);</span><br><span class="line"></span><br><span class="line">long func01(int n)</span><br><span class="line">&#123;</span><br><span class="line">    long m;</span><br><span class="line">    if (n &gt; 0)</span><br><span class="line">    &#123;</span><br><span class="line">        m = n * func01(n-1);</span><br><span class="line">    &#125;</span><br><span class="line">    else</span><br><span class="line">    &#123;</span><br><span class="line">        m = 1;</span><br><span class="line">    &#125;</span><br><span class="line">    return m;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">    long int a = func01(5);</span><br><span class="line">    printf(&quot;%d\n&quot;, a);</span><br><span class="line">    long int b = func01(5);</span><br><span class="line">    printf(&quot;%d\n&quot;, b);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h2><p>​	快速排序算法的基本思想是：通过一趟排序将待排序数据分割成独立的两个部分，其中一部分的所有元素均比另一部分的元素小，然后分别对这两部分继续进行排序，重复上述步骤直到排序完成。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">print_list</span><span class="params">(<span class="type">int</span> *int01, <span class="type">int</span> length)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; length; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, *int01++);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">quick_sort</span><span class="params">(<span class="type">int</span> <span class="built_in">array</span>[], <span class="type">int</span> left, <span class="type">int</span> right)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i = left, j = right;</span><br><span class="line">    <span class="type">int</span> temp;</span><br><span class="line">    <span class="type">int</span> pivot;</span><br><span class="line">    pivot = <span class="built_in">array</span>[(left + right) / <span class="number">2</span>];</span><br><span class="line">    <span class="keyword">while</span> (i &lt; j)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 从左到右找到大于等于基准点的元素</span></span><br><span class="line">        <span class="keyword">while</span> (<span class="built_in">array</span>[i] &lt; pivot)</span><br><span class="line">        &#123;</span><br><span class="line">            i++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 从右到左找到小于等于基准点的元素</span></span><br><span class="line">        <span class="keyword">while</span> (<span class="built_in">array</span>[j] &gt; pivot)</span><br><span class="line">        &#123;</span><br><span class="line">            j--;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;currnt: %d,%d\n&quot;</span>, i, j);</span><br><span class="line">        <span class="comment">// 如果i&lt;=j，则互换</span></span><br><span class="line">        <span class="keyword">if</span> (i &lt;= j)</span><br><span class="line">        &#123;</span><br><span class="line">            temp = <span class="built_in">array</span>[i];</span><br><span class="line">            <span class="built_in">array</span>[i] = <span class="built_in">array</span>[j];</span><br><span class="line">            <span class="built_in">array</span>[j] = temp;</span><br><span class="line">            i++;</span><br><span class="line">            j--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    print_list(&amp;<span class="built_in">array</span>[<span class="number">0</span>], <span class="number">9</span>);</span><br><span class="line">    <span class="keyword">if</span> (left &lt; j)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n  sort right array[%d,%d]\n&quot;</span>, left, j);</span><br><span class="line">        quick_sort(<span class="built_in">array</span>, left, j);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (i &lt; right)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n  sort left array[%d,%d]\n&quot;</span>, i, right);</span><br><span class="line">        quick_sort(<span class="built_in">array</span>, i, right);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> <span class="built_in">array</span>[] = &#123;<span class="number">9</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">2</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>&#125;;</span><br><span class="line">    <span class="type">int</span> i, length;</span><br><span class="line"></span><br><span class="line">    length = <span class="keyword">sizeof</span>(<span class="built_in">array</span>) / <span class="keyword">sizeof</span>(<span class="built_in">array</span>[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n  sort all array[%d,%d]\n&quot;</span>, <span class="number">0</span>, length<span class="number">-1</span>);</span><br><span class="line">    quick_sort(<span class="built_in">array</span>, <span class="number">0</span>, length - <span class="number">1</span>);</span><br><span class="line">    print_list(&amp;<span class="built_in">array</span>[<span class="number">0</span>], length);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="内存管理函数"><a href="#内存管理函数" class="headerlink" title="内存管理函数"></a>内存管理函数</h2><ol>
<li><p>malloc   申请动态内存空间  viod *malloc(size_t size);  在堆上，需要手动释放。</p>
</li>
<li><p>free        释放动态内存空间  void free(void *ptr);释放堆上的内存</p>
</li>
<li><p>calloc     申请并初始化一系列内存空间  void *calloc(size_t nmemb, size_t size);初始为0。</p>
</li>
<li><p>realloc   重新分配内存空间     viod *realloc(void *ptr, size_t size);重新分配并拷贝。</p>
<p>（注明： 普通的局部变量在栈上。）</p>
</li>
<li><p>memset     使用一个常量字节填充内存空间    memset(ptr, 0, N * sizeof(int)); </p>
</li>
<li><p>memcpy      拷贝内存空间                                 memcpy(prt2,prt1,N);  free(ptr1);</p>
</li>
<li><p>memmove   拷贝内存空间</p>
</li>
<li><p>memcmp      比较内存空间</p>
</li>
<li><p>memchr        在内存空间中搜索一个字符</p>
</li>
</ol>
<p>根据用户输入动态扩容内存的例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdlib.h&gt;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;string.h&gt;</span></span><br><span class="line"></span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    int num, i;</span><br><span class="line">    int count = 0;</span><br><span class="line">    int *ptr = NULL;</span><br><span class="line">    do</span><br><span class="line">    &#123;</span><br><span class="line">        printf(&quot;input number: &quot;);</span><br><span class="line">        scanf(&quot;%d&quot;, &amp;num);</span><br><span class="line">        count++;</span><br><span class="line">        ptr = (int *)realloc(ptr, count * sizeof(int));</span><br><span class="line">        if (ptr == NULL)</span><br><span class="line">        &#123;</span><br><span class="line">            exit(1);</span><br><span class="line">        &#125;</span><br><span class="line">        ptr[count - 1] = num;</span><br><span class="line">    &#125; while (num != -1);</span><br><span class="line"></span><br><span class="line">    for (i = 0; i &lt; count; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        printf(&quot;%d &quot;, ptr[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    putchar(&#x27;\n&#x27;);</span><br><span class="line">    free(ptr);</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="内存布局"><a href="#内存布局" class="headerlink" title="内存布局"></a>内存布局</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdlib.h&gt;</span></span><br><span class="line"></span><br><span class="line">int global_uninit_var;</span><br><span class="line">int global_init_var1 = 520 ;</span><br><span class="line">int global_init_var2 = 880 ;</span><br><span class="line"></span><br><span class="line">void func(void);</span><br><span class="line">void func(void)&#123;</span><br><span class="line">	;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(void)&#123;</span><br><span class="line">	int local_var1;</span><br><span class="line">	int local_var2;</span><br><span class="line">	</span><br><span class="line">	static int static_uninit_var;</span><br><span class="line">	static int static_init_var = 456;</span><br><span class="line">	</span><br><span class="line">	char *str1 = &quot;1111&quot;;                   //字符串常量</span><br><span class="line">	char *str2 = &quot;2222&quot;;</span><br><span class="line">	char str3[5] = &#123;&#x27;a&#x27;,&#x27;b&#x27;,&#x27;1&#x27;,&#x27;2&#x27;,&#x27;\0&#x27;&#125;; //局部变量</span><br><span class="line">	char str4[5] = &#123;&#x27;a&#x27;,&#x27;b&#x27;,&#x27;1&#x27;,&#x27;2&#x27;,&#x27;\0&#x27;&#125;; </span><br><span class="line">	</span><br><span class="line">	int *malloc_var = (int *)malloc(sizeof(int));</span><br><span class="line">	</span><br><span class="line">    printf(&quot;addr of local_var1 -&gt;        %p\n&quot;,&amp;local_var1);</span><br><span class="line">	printf(&quot;addr of local_var2 -&gt;        %p\n&quot;,&amp;local_var2);</span><br><span class="line">    printf(&quot;addr of str3 -&gt;              %p\n&quot;,str3);</span><br><span class="line">    printf(&quot;addr of str4 -&gt;              %p\n&quot;,str4);</span><br><span class="line">	printf(&quot;addr of malloc_var -&gt;        %p\n&quot;,malloc_var);</span><br><span class="line">	printf(&quot;addr of global_uninit_var -&gt; %p\n&quot;,&amp;global_uninit_var);</span><br><span class="line">	printf(&quot;addr of static_uninit_var -&gt; %p\n&quot;,&amp;static_uninit_var);</span><br><span class="line">	printf(&quot;addr of static_init_var -&gt;   %p\n&quot;,&amp;static_init_var);</span><br><span class="line">	printf(&quot;addr of global_init_var2 -&gt;  %p\n&quot;,&amp;global_init_var2);</span><br><span class="line">	printf(&quot;addr of global_init_var1 -&gt;  %p\n&quot;,&amp;global_init_var1);</span><br><span class="line">	printf(&quot;addr of str2 -&gt;              %p\n&quot;,str2);</span><br><span class="line">	printf(&quot;addr of str1 -&gt;              %p\n&quot;,str1);</span><br><span class="line">	printf(&quot;addr of func -&gt;              %p\n&quot;,func);</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">[root@wei07 c]# gcc t.c -o t &amp;&amp; ./t</span><br><span class="line">addr of local_var1 -&gt;        0x7ffc0d64f00c</span><br><span class="line">addr of local_var2 -&gt;        0x7ffc0d64f008</span><br><span class="line">addr of str3 -&gt;              0x7ffc0d64f000</span><br><span class="line">addr of str4 -&gt;              0x7ffc0d64eff0</span><br><span class="line">addr of malloc_var2 -&gt;       0x1d32030</span><br><span class="line">addr of malloc_var1 -&gt;       0x1d32010</span><br><span class="line">addr of global_uninit_var -&gt; 0x601050</span><br><span class="line">addr of static_uninit_var -&gt; 0x60104c</span><br><span class="line">addr of static_init_var -&gt;   0x601044</span><br><span class="line">addr of global_init_var2 -&gt;  0x601040</span><br><span class="line">addr of global_init_var1 -&gt;  0x60103c</span><br><span class="line">addr of str2 -&gt;              0x4007a5</span><br><span class="line">addr of str1 -&gt;              0x4007a0</span><br><span class="line">addr of func -&gt;              0x40057d</span><br><span class="line">[root@wei07 c]#</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<table>
<thead>
<tr>
<th>变量名称</th>
<th>变量类型</th>
<th>内存地址</th>
<th>内存段</th>
</tr>
</thead>
<tbody><tr>
<td>local_var1</td>
<td>局部变量</td>
<td>高（大）</td>
<td>栈stack</td>
</tr>
<tr>
<td>local_var2</td>
<td>局部变量</td>
<td></td>
<td>栈stack</td>
</tr>
<tr>
<td>str3[]</td>
<td>局部变量</td>
<td></td>
<td>栈stack</td>
</tr>
<tr>
<td>str4[]</td>
<td>局部变量</td>
<td></td>
<td>栈stack</td>
</tr>
<tr>
<td>malloc_var2</td>
<td>动态申请的内存空间</td>
<td></td>
<td>堆heap</td>
</tr>
<tr>
<td>malloc_var1</td>
<td>动态申请的内存空间</td>
<td></td>
<td>堆heap</td>
</tr>
<tr>
<td>global_uninit_var</td>
<td>全局变量（未初始化）</td>
<td></td>
<td>BSS</td>
</tr>
<tr>
<td>static_uninit_var</td>
<td>静态变量（未初始化）</td>
<td></td>
<td>BSS</td>
</tr>
<tr>
<td>static_init_var</td>
<td>静态变量（初始化）</td>
<td></td>
<td>数据段</td>
</tr>
<tr>
<td>global_init_var2</td>
<td>全局变量（初始化）</td>
<td></td>
<td>数据段</td>
</tr>
<tr>
<td>global_init_var1</td>
<td>全局变量（初始化）</td>
<td></td>
<td>数据段</td>
</tr>
<tr>
<td>str2</td>
<td>字符串常量</td>
<td></td>
<td>代码段</td>
</tr>
<tr>
<td>str1</td>
<td>字符串常量</td>
<td></td>
<td>代码段</td>
</tr>
<tr>
<td>func</td>
<td>函数</td>
<td>低（小）</td>
<td>代码段</td>
</tr>
</tbody></table>
<p>注明1：BSS是英文Block Started by Symbol的简称，这个区段的数据在程序运行前将被自动初始化为数字0.</p>
<p>注明2：linux命令size，显示程序的text，data，bss段的大小。程序的本质就是这3个段。</p>
<p>注明3：堆和栈的区别 （ 堆由程序员手动申请，栈由系统自动分配；堆到主动释放时结束，栈到局部函数返回时结束；不同函数可以自由访问堆，不同函数不能相互访问栈；堆由低往高用，栈由高往低用，他们共享一片蓝天。）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">D:\soft\src\Python3\C&gt;gcc t.c -o t &amp;&amp; t</span><br><span class="line">addr of local_var1 -&gt;        000000000061FDFC</span><br><span class="line">addr of local_var2 -&gt;        000000000061FDF8</span><br><span class="line">addr of str3 -&gt;              000000000061FDF3</span><br><span class="line">addr of str4 -&gt;              000000000061FDEE</span><br><span class="line">addr of malloc_var2 -&gt;       0000000000736BD0</span><br><span class="line">addr of malloc_var1 -&gt;       0000000000736BB0</span><br><span class="line">addr of global_uninit_var -&gt; 0000000000407970</span><br><span class="line">addr of static_uninit_var -&gt; 0000000000407030</span><br><span class="line">addr of static_init_var -&gt;   0000000000403018</span><br><span class="line">addr of global_init_var2 -&gt;  0000000000403014</span><br><span class="line">addr of global_init_var1 -&gt;  0000000000403010</span><br><span class="line">addr of str2 -&gt;              0000000000404005</span><br><span class="line">addr of str1 -&gt;              0000000000404000</span><br><span class="line">addr of func -&gt;              0000000000401550</span><br><span class="line"></span><br><span class="line">D:\soft\src\Python3\C&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="UNIX接口-低级IO-read和write"><a href="#UNIX接口-低级IO-read和write" class="headerlink" title="UNIX接口-低级IO-read和write"></a>UNIX接口-低级IO-read和write</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;stdio.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;unistd.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BUF 6</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">copy01</span><span class="params">()</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar01</span><span class="params">()</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar02</span><span class="params">()</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    copy01();</span><br><span class="line"></span><br><span class="line">    <span class="type">char</span> c;</span><br><span class="line">    c = getchar01();</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line">    c = getchar01(); <span class="comment">// 等待read</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line"></span><br><span class="line">    c = getchar02();</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02(); <span class="comment">// 要么从缓存里面取，要么等待read</span></span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02();</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注明： </span></span><br><span class="line">    <span class="comment">//      如果把getchar02放到01前面执行，02会一次性尽量读取BUFSIZE512个字符，</span></span><br><span class="line">    <span class="comment">//      会发现01要等待第二次的标准输入。</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 复制标准输入到标准输出 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">copy01</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span> exit0 = <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">    <span class="type">char</span> buf[BUF];</span><br><span class="line">    <span class="type">int</span> n;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入内容，回车结束。输入0时，退出复制。\n&quot;</span>);</span><br><span class="line">    <span class="keyword">while</span> ((n = read(<span class="number">0</span>, buf, BUF)) &gt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (buf[<span class="number">0</span>] == exit0)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;退出。\n&quot;</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        write(<span class="number">1</span>, buf, n);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 不带缓冲的getchar */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar01</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span> c;</span><br><span class="line">    <span class="keyword">return</span> (read(<span class="number">0</span>, &amp;c, <span class="number">1</span>) == <span class="number">1</span>) ? (<span class="type">unsigned</span> <span class="type">char</span>)c : EOF;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 带缓冲的getchar */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar02</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">static</span> <span class="type">char</span> buf[BUFSIZ];</span><br><span class="line">    <span class="type">static</span> <span class="type">char</span> *bufp = buf;</span><br><span class="line">    <span class="type">static</span> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (n == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        n = read(<span class="number">0</span>, buf, <span class="keyword">sizeof</span>(buf));</span><br><span class="line">        bufp = buf;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> (--n &gt;= <span class="number">0</span>) ? (<span class="type">unsigned</span> <span class="type">char</span>)*bufp++ : EOF;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="共用体union"><a href="#共用体union" class="headerlink" title="共用体union"></a>共用体union</h2><p>​		特殊的结构体，里面的所有成员共用同一个地址，同一时间只能用里面的一个成员。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">Test</span>&#123;</span></span><br><span class="line">    <span class="type">int</span> t;</span><br><span class="line">    <span class="type">double</span> pi;</span><br><span class="line">    <span class="type">char</span> str[<span class="number">6</span>];</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="时间tm结构"><a href="#时间tm结构" class="headerlink" title="时间tm结构"></a>时间tm结构</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;time.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>&#123;</span><br><span class="line">    strcut tm *p;</span><br><span class="line">    <span class="type">time_t</span> t;</span><br><span class="line">    time(&amp;t);          <span class="comment">//秒数赋值</span></span><br><span class="line">    p = localtime(&amp;t); <span class="comment">//tm结构体赋值</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59        */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59        */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23        */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31    */</span><br><span class="line">   int tm_mon;         /* 月，范围从 0 到 11        */</span><br><span class="line">   int tm_year;        /* 自 1900 年起的年数        */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6    */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365    */</span><br><span class="line">   int tm_isdst;       /* 夏令时                */</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="枚举类型enum"><a href="#枚举类型enum" class="headerlink" title="枚举类型enum"></a>枚举类型enum</h2><p>​		如果一个变量有且仅有几种值，那么就适合用枚举类型。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Week</span> &#123;</span>sun,mon,tue,wed,thu,fri,sat&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Week</span> <span class="title">today</span>;</span></span><br><span class="line">    strunt tm *p;</span><br><span class="line">    <span class="type">time_t</span> t;</span><br><span class="line">    time(&amp;t);</span><br><span class="line">    p=localtime(&amp;t);</span><br><span class="line">    today=p-&gt;tm_wday;</span><br><span class="line">    <span class="keyword">return</span> today;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>注明：枚举就是符号常量，默认0，1，2，3…;  可以定义枚举类型的时候修改默认值，修改后呈现两面派：0，1，12，13…</p>
<h2 id="位域-节约空间"><a href="#位域-节约空间" class="headerlink" title="位域(:节约空间)"></a>位域(:节约空间)</h2><p>​		位域是一个成员站用几个2进制位。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Test</span>&#123;</span></span><br><span class="line">	<span class="type">unsigned</span> <span class="type">int</span> a:<span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b:<span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c:<span class="number">2</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="位运算"><a href="#位运算" class="headerlink" title="位运算"></a>位运算</h2><ul>
<li><p>~   按位取反</p>
</li>
<li><p>&amp;   按位与</p>
</li>
<li><p>^   按位非</p>
</li>
<li><p>|   按位或</p>
<p>注明：配合掩码使用很好。</p>
</li>
</ul>
<h2 id="移位运算"><a href="#移位运算" class="headerlink" title="移位运算"></a>移位运算</h2><p>​	value &lt;&lt; 2 ; 左移2位 </p>
<p>​    value &gt;&gt; 2 ; 右移2位</p>
<h2 id="打开和关闭文件"><a href="#打开和关闭文件" class="headerlink" title="打开和关闭文件"></a>打开和关闭文件</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">FILE *fp = fopen(info-&gt;software_fname,<span class="string">&quot;w&quot;</span>);</span><br><span class="line"><span class="built_in">fputs</span>(<span class="string">&quot;Enter the name of the software package manually.\n&quot;</span>,fp);</span><br><span class="line">fclose(fp);</span><br></pre></td></tr></table></figure>

<p>​		参考网站： <a target="_blank" rel="noopener" href="https://fishc.com.cn/thread-90808-1-1.html">https://fishc.com.cn/thread-90808-1-1.html</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数原型</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line">...</span><br><span class="line">FILE *<span class="title function_">fopen</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *path, <span class="type">const</span> <span class="type">char</span> *mode)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>参数解析：</strong></p>
<table>
<thead>
<tr>
<th><strong>参数</strong></th>
<th><strong>含义</strong></th>
</tr>
</thead>
<tbody><tr>
<td>path</td>
<td>该参数是一个 C 语言字符串，指定了待打开的文件路径和文件名</td>
</tr>
<tr>
<td>mode</td>
<td>“r” …如下</td>
</tr>
</tbody></table>
<p>“r”  1. 以只读的模式打开一个文本文件，从文件头开始读取  2. 该文本文件必须存在 </p>
<p>“w”1. 以只写的模式打开一个文本文件，从文件头开始写入  2. 如果文件不存在则创建一个新的文件  3. 如果文件已存在则将文件的长度截断为 0（重新写入的内容将覆盖原有的所有内容） </p>
<p>“a”1. 以追加的模式打开一个文本文件，从文件末尾追加内容  2. 如果文件不存在则创建一个新的文件</p>
<p>“r+”1. 以读和写的模式打开一个文本文件，从文件头开始读取和写入  2. 该文件必须存在  3. 该模式不会将文件的长度截断为 0（只覆盖重新写入的内容，原有的内容保留） </p>
<p>“w+”1. 以读和写的模式打开一个文本文件，从文件头开始读取和写入  2. 如果文件不存在则创建一个新的文件  3. 如果文件已存在则将文件的长度截断为 0（重新写入的内容将覆盖原有的所有内容） </p>
<p>“a+”1. 以读和追加的模式打开一个文本文件  2. 如果文件不存在则创建一个新的文件  3. 读取是从文件头开始，而写入则是在文件末尾追加</p>
<p>“b”1. 与上面 6 中模式均可结合（”rb”, “wb”, “ab”, “r+b”, “w+b”, “a+b”）  2. 其描述的含义一样，只不过操作的对象是二进制文件</p>
<p><strong>返回值：</strong></p>
<ol>
<li><p>如果文件打开成功，则返回一个指向 FILE 结构的文件指针；</p>
</li>
<li><p>如果文件打开失败，则返回 NULL 并设置 errno 为指定的错误。</p>
</li>
</ol>
<h2 id="顺序读写文件"><a href="#顺序读写文件" class="headerlink" title="顺序读写文件"></a>顺序读写文件</h2><h4 id="读写单个字符"><a href="#读写单个字符" class="headerlink" title="读写单个字符"></a>读写单个字符</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fgetc</span><span class="params">(FILE *_File)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fputc</span><span class="params">(<span class="type">int</span> _Ch,FILE *_File)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="读写字符串"><a href="#读写字符串" class="headerlink" title="读写字符串"></a>读写字符串</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> *__cdecl <span class="title function_">fgets</span><span class="params">(<span class="type">char</span> * __restrict__ _Buf,<span class="type">int</span> _MaxCount,FILE * __restrict__ _File)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fputs</span><span class="params">(<span class="type">const</span> <span class="type">char</span> * __restrict__ _Str,FILE * __restrict__ _File)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>​		注明1：_MaxCount是字符数量+1，因为末尾自动添加’\0’再返回。</p>
<p>​		注明2：如果没有到达_MaxCount就遇到了’\n’，则只读取到’\n’。</p>
<h4 id="格式化读写文件"><a href="#格式化读写文件" class="headerlink" title="格式化读写文件"></a>格式化读写文件</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fprintf</span><span class="params">(FILE * __restrict__ _File,<span class="type">const</span> <span class="type">char</span> * __restrict__ _Format,...)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fscanf</span><span class="params">(FILE * __restrict__ _File,<span class="type">const</span> <span class="type">char</span> * __restrict__ _Format,...)</span> __MINGW_ATTRIB_DEPRECATED_SEC_WARN;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="指定尺寸读写文件"><a href="#指定尺寸读写文件" class="headerlink" title="指定尺寸读写文件"></a>指定尺寸读写文件</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">size_t</span> __cdecl <span class="title function_">fread</span><span class="params">(<span class="type">void</span> * __restrict__ _DstBuf,<span class="type">size_t</span> _ElementSize,<span class="type">size_t</span> _Count,FILE * __restrict__ _File)</span>;</span><br><span class="line"><span class="type">size_t</span> __cdecl <span class="title function_">fwrite</span><span class="params">(<span class="type">const</span> <span class="type">void</span> * __restrict__ _Str,<span class="type">size_t</span> _Size,<span class="type">size_t</span> _Count,FILE * __restrict__ _File)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="随机读写文件"><a href="#随机读写文件" class="headerlink" title="随机读写文件"></a>随机读写文件</h2><ul>
<li><p>获取位置指示器的位置 </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">long</span> __cdecl <span class="title function_">ftell</span><span class="params">(FILE *_File)</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>设置位置指示器的位置</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fseek</span><span class="params">(FILE *_File,<span class="type">long</span> _Offset,<span class="type">int</span> _Origin)</span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> SEEK_CUR 1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SEEK_END 2</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SEEK_SET 0</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="错误号打印"><a href="#错误号打印" class="headerlink" title="错误号打印"></a>错误号打印</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;errno.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>&#123;</span><br><span class="line">    FILE *fp;</span><br><span class="line">    <span class="keyword">if</span> ((fp = fopen(<span class="string">&quot;aaa.txt&quot;</span>,<span class="string">&quot;r&quot;</span>)) == <span class="literal">NULL</span>)&#123;</span><br><span class="line">        perror(<span class="string">&quot;打开文件失败&quot;</span>);</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>,<span class="string">&quot;错误：%s \n打开文件失败。\n&quot;</span>,strerror(errno));</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    fclose(fp);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​		注明： perror 和 strerror(errno) ，二选一。</p>
<h2 id="IO缓冲区"><a href="#IO缓冲区" class="headerlink" title="IO缓冲区"></a>IO缓冲区</h2><ul>
<li>手动刷新缓冲区：  fflush</li>
<li>设置缓冲区参数：  setvbuf<ul>
<li>按块缓存  _IOFBF</li>
<li>按行缓存  _IOLBF</li>
<li>不缓存      _IONBF</li>
</ul>
</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fflush</span><span class="params">(FILE *_File)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">setvbuf</span><span class="params">(FILE * __restrict__ _File,<span class="type">char</span> * __restrict__ _Buf,<span class="type">int</span> _Mode,<span class="type">size_t</span> _Size)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h1 id="扩展"><a href="#扩展" class="headerlink" title="扩展"></a>扩展</h1><h2 id="C语言获取Linux系统的CPU的主频"><a href="#C语言获取Linux系统的CPU的主频" class="headerlink" title="C语言获取Linux系统的CPU的主频"></a>C语言获取Linux系统的CPU的主频</h2><h4 id="直接读系统文件"><a href="#直接读系统文件" class="headerlink" title="直接读系统文件"></a>直接读系统文件</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">        FILE *fp;</span><br><span class="line">        <span class="type">char</span> str[<span class="number">81</span>];</span><br><span class="line">        <span class="built_in">memset</span>(str,<span class="number">0</span>,<span class="number">81</span>);</span><br><span class="line">        fp=popen(<span class="string">&quot;cat /proc/cpuinfo|grep cpu\\ MHz|sed -e &#x27;s/.*:[^0-9]//&#x27;&quot;</span>,<span class="string">&quot;r&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span>(fp&lt;<span class="number">0</span>)&#123;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;无法读取 CPU主频信息\n&quot;</span>);</span><br><span class="line">                <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        fgets(str,<span class="number">80</span>,fp);</span><br><span class="line">        fclose(fp);</span><br><span class="line">        <span class="type">double</span> spd=atof(str);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;您 CPU的 主频是%fMHz\n&quot;</span>,spd);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="用C语言测试计算频率"><a href="#用C语言测试计算频率" class="headerlink" title="用C语言测试计算频率"></a>用C语言测试计算频率</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;inttypes.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/time.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">int64_t</span> <span class="title function_">rdtsc</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> i, j;</span><br><span class="line">    <span class="keyword">asm</span> <span class="title function_">volatile</span> <span class="params">(<span class="string">&quot; rdtsc&quot;</span> : <span class="string">&quot;=a&quot;</span>(i), <span class="string">&quot;=d&quot;</span>(j) : )</span>;</span><br><span class="line">    <span class="keyword">return</span> ((<span class="type">int64_t</span>)j&lt;&lt;<span class="number">32</span>) + (<span class="type">int64_t</span>)i;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int64_t</span> tsc_start, tsc_end;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">timeval</span> <span class="title">tv_start</span>, <span class="title">tv_end</span>;</span></span><br><span class="line">    <span class="type">int</span> usec_delay;</span><br><span class="line"></span><br><span class="line">    tsc_start = rdtsc();</span><br><span class="line">    gettimeofday(&amp;tv_start, <span class="literal">NULL</span>);</span><br><span class="line">    usleep(<span class="number">100000</span>);</span><br><span class="line">    tsc_end = rdtsc();</span><br><span class="line">    gettimeofday(&amp;tv_end, <span class="literal">NULL</span>);</span><br><span class="line"></span><br><span class="line">    usec_delay = <span class="number">1000000</span> * (tv_end.tv_sec - tv_start.tv_sec)</span><br><span class="line">        + (tv_end.tv_usec - tv_start.tv_usec);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot; cpu MHz\t\t: %.3f\n&quot;</span>,</span><br><span class="line">           (<span class="type">double</span>)(tsc_end-tsc_start) / usec_delay);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[root@wei07 c]#</span><br><span class="line">[root@wei07 c]# gcc c.c -o c &amp;&amp; ./c</span><br><span class="line"> cpu MHz                : 2112.022</span><br><span class="line">[root@wei07 c]#</span><br><span class="line">[root@wei07 c]#</span><br><span class="line">[root@wei07 c]# cat /proc/cpuinfo|grep cpu\ MHz</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">[root@wei07 c]#</span><br><span class="line"></span><br></pre></td></tr></table></figure>



 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2023/05/23/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E7%AC%94%E8%AE%B0202305/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%9F%BA%E7%A1%80/" rel="tag">基础</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2023/06/15/ESQLC%E6%B5%8B%E8%AF%95GBase8s%E6%B8%B8%E6%A0%87%E7%A8%B3%E5%AE%9A%E6%80%A7%E8%AF%BB/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            ESQLC测试GBase8s游标稳定性读
          
        </div>
      </a>
    
    
      <a href="/2023/05/10/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E8%BF%94%E5%9B%9E%E5%80%BC/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">C语言：函数指针函数作为参数函数作为返回值</div>
      </a>
    
  </nav>

  
   
  
    
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023-2024
        <i class="ri-heart-fill heart_icon"></i> Wang Wei
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ww.ico" alt="朝花夕拾"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>